<?php

class MemberAction extends Action {

    public function login() {
        if ($_POST) {
			if(!$_POST['username'] or !$_POST['password']){
				$this->error('不能为空');
			}
			$userInfo = M('user')->where(array('username'=>$_POST['username']))->find();
			if($userInfo)
			{
				if($userInfo['password'] == md5($_POST['password']))
				{
					$_SESSION['m_user'] = $userInfo;
				}
				$this->redirect('Index/index');
			}else{
				$this->error('无此用户');
			}
			
        } else {
            $this->display();
        }
    }

    public function register() {
		if($_POST)
		{
			if(!$_POST['username'] or !$_POST['password']){
				$this->error('不能为空');
			}
			if(M('user')->where(array('username'=>$_POST['username']))->count())
			{
				$this->error('用户名已存在');
			}
			$user_id = M('user')->add(array('username'=>$_POST['username'],'password'=>md5($_POST['password']),'jifen'=>0));
			$_SESSION['m_user']['id']=$user_id;
			$_SESSION['m_user']['username']=$_POST['username'];
			$_SESSION['m_user']['password']=md5($_POST['password']);
			$_SESSION['m_user']['jifen']=0;
			$this->redirect('Index/index');
		}else{
			$this->display();
		}
        
    }

	public function logout()
	{
		session('m_user',null);
		$this->redirect('Index/index');
	}

}